/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.sober.transaction;

import com.jspx.sober.SoberEnv;
import com.jspx.sober.SoberSupport;
import com.jspx.sober.util.JdbcUtil;
import com.jspx.sober.exception.TransactionException;
import javax.transaction.*;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 2008-8-5
 * Time: 16:15:52
 */
public class JTATransaction extends AbstractTransaction
{
    private JTATransaction()
    {

    }

    public JTATransaction(SoberSupport soberSupport)
    {
        this.setSoberSupport(soberSupport);
    }

    private UserTransaction userTransaction = null;
    public UserTransaction getUserTransaction()
    {
        return userTransaction;
    }

    public void setUserTransaction(UserTransaction userTransaction)
    {
        this.userTransaction = userTransaction;
    }

    public void begin() throws TransactionException
    {
        isActive++;
        if (isActive==1)
        try
        {
            userTransaction.setTransactionTimeout(timeout);
            userTransaction.begin();
        } catch (SystemException e)
        {
            throw new TransactionException(e,"Error Transaction begin,JTA事务不能够开启,检查系统环境");
        } catch(NotSupportedException e)
        {
            throw new TransactionException(e,"Error Transaction begin,JTA事务不能够开启,系统不支持");
        }  finally {

            wasCommitted = false;
            wasRolledBack = false;
        }
    }
    public void commit() throws TransactionException
    {
        isActive--;
        if (isActive > 0) return;
        if (wasCommitted  || wasRolledBack) return;
        try
        {
            userTransaction.commit();
        } catch (Exception e)
        {
            JdbcUtil.closeConnection(connection,true);
            throw new TransactionException(e,"Error Transaction commit,JTA事务不能提交,回滚出错");
        } finally {
            transactionManager.remove(soberSupport.getTid());
            soberSupport.setTid(SoberEnv.notTransaction);
            wasCommitted = true;
            isActive = 0;
            JdbcUtil.closeConnection(connection,false);
        }
    }
    public void rollback() throws TransactionException
    {
        if (wasRolledBack) return;
        isActive = 0;
        try
        {
            userTransaction.rollback();
        } catch (SystemException e)
        {
            throw new TransactionException(e,"Error Transaction rollback,JTA事务不能回滚,可能是环境错误");
        } finally
        {
            transactionManager.remove(soberSupport.getTid());
            soberSupport.setTid(SoberEnv.notTransaction);
            wasRolledBack = true;
            JdbcUtil.closeConnection(connection,true);
        }

    }
}